<template>
  <el-card class="box-card" style="margin: 10px 0">
    <div slot="header" class="clearfix">
      <!-- 头部左侧内容 -->
      <!-- @tab-click="handleClick" -->
      <el-tabs v-model="activeName">
        <el-tab-pane label="销售额" name="sale"></el-tab-pane>
        <el-tab-pane label="访问量" name="visit"></el-tab-pane>
      </el-tabs>
      <!-- 头部右侧内容 -->
      <div class="right">
        <span class="setDate" @click="setToday">今日</span>
        <span class="setDate" @click="setThisWeek">本周</span>
        <span class="setDate" @click="setThisMonth">本月</span>
        <span class="setDate" @click="setThisYear">本年</span>
        <el-date-picker
          style="width: 250px"
          v-model="date"
          value-format="yyyy-MM-dd"
          class="date"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="mini"
        >
        </el-date-picker>
      </div>
    </div>
    <div>
      <el-row :gutter="10">
        <el-col :span="18">
          <div class="charts" ref="charts"></div>
        </el-col>
        <el-col :span="6">
          <h3>门店{{ this.title }}排名</h3>
          <ul>
            <li>
              <span class="rindex">1</span>
              <span>肯德基</span>
              <span class="rvalue">123456</span>
            </li>
            <li>
              <span class="rindex">2</span>
              <span>肯德基</span>
              <span class="rvalue">123456</span>
            </li>
            <li>
              <span class="rindex">3</span>
              <span>肯德基</span>
              <span class="rvalue">123456</span>
            </li>
            <li>
              <span class="rindex">4</span>
              <span>肯德基</span>
              <span class="rvalue">123456</span>
            </li>
            <li>
              <span class="rindex">5</span>
              <span>肯德基</span>
              <span class="rvalue">123456</span>
            </li>
            <li>
              <span class="rindex">6</span>
              <span>肯德基</span>
              <span class="rvalue">123456</span>
            </li>
            <li>
              <span class="rindex">7</span>
              <span>肯德基</span>
              <span class="rvalue">123456</span>
            </li>
          </ul>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script>
import * as echarts from "echarts";
import dayjs from "dayjs";
import { mapState } from "vuex";
export default {
  name: "Sale",
  data() {
    return {
      activeName: "sale",
      mycharts: null,
      date: [], // 日期数据
    };
  },
  computed: {
    // 标题
    title() {
      return this.activeName === "sale" ? "销售额" : "访问量";
    },
    ...mapState({ list: (state) => state.home.list }),
  },
  watch: {
    activeName(newVal) {
      const { list } = this;
      this.mycharts.setOption({
        title: {
          text: this.title,
        },
        xAxis: {
          data:
            newVal === "sale" ? list.orderFullYearAxis : list.userFullYearAxis,
        },
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "45%",
            data: newVal === "sale" ? list.orderFullYear : list.userFullYear,
          },
        ],
      });
    },
    list(newVal) {
      const { list } = this;
      this.mycharts.setOption({
        title: {
          text: this.title,
        },
        xAxis: {
          data: newVal.orderFullYearAxis,
        },
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "45%",
            data: newVal.orderFullYear,
          },
        ],
      });
    },
  },
  methods: {
    setToday() {
      const today = dayjs().format("YYYY-MM-DD");
      this.date = [today, today];
    },
    setThisWeek() {
      const start = dayjs().day(1).format("YYYY-MM-DD");
      const end = dayjs().day(7).format("YYYY-MM-DD");
      this.date = [start, end];
    },
    setThisMonth() {
      const start = dayjs().startOf("month").format("YYYY-MM-DD");
      const end = dayjs().endOf("month").format("YYYY-MM-DD");
      this.date = [start, end];
    },
    setThisYear() {
      const start = dayjs().startOf("year").format("YYYY-MM-DD");
      const end = dayjs().endOf("year").format("YYYY-MM-DD");
      this.date = [start, end];
    },
  },
  mounted() {
    // 初始化echarts
    this.mycharts = echarts.init(this.$refs.charts);
    this.mycharts.setOption({
      title: {
        text: "销售额趋势",
        textStyle: {
          fontSize: 16,
        },
      },
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          data: [],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          name: "Direct",
          type: "bar",
          barWidth: "45%",
          data: [],
        },
      ],
    });

    let timer = null;
    window.addEventListener("resize", () => {
      clearInterval(timer);
      timer = setTimeout(() => {
        this.mycharts.resize();
      }, 50);
    });
  },
};
</script>

<style scoped>
.clearfix {
  position: relative;
}
.right {
  position: absolute;
  right: 0;
  top: 0;
}
.date {
  width: 200px;
  margin: 0 20px;
}
.right span {
  margin: 0 10px;
  font-size: 14px;
}
.charts {
  width: 100%;
  height: 300px;
}
.el-col-6 > h3 {
  font-size: 16px;
}
ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0;
}
ul li {
  height: 8%;
  margin: 10px 0;
}
.rindex {
  display: inline-block;
  width: 18px;
  height: 18px;
  padding-left: 4px;
  margin-right: 20px;
}

ul > li:nth-child(-n + 3) > .rindex {
  border-radius: 50%;
  background-color: black;
  color: white;
}
.rvalue {
  float: right;
}
.setDate:hover {
  cursor: pointer;
  color: blue;
}
</style>
